<template>
    <div style="width: 600px">
        <el-input
                placeholder="输入关键字进行过滤"
                v-model="filterText">
        </el-input>

        <el-tree
                :data="deps"
                :props="defaultProps"
                :filter-node-method="filterNode"
                :expand-on-click-node="false"
                ref="tree">
                <span class="custom-tree-node" slot-scope="{ node, data }" style="display: flex;justify-content: space-between;width: 100%">
                    <span>{{ data.name }}</span>
                    <span>
                      <el-button
                              type="primary"
                              size="mini"
                              class="depNode"
                              @click="() => showAddDep(data)">
                        添加
                      </el-button>
                      <el-button
                              type="primary"
                              size="mini"
                              class="depNode"
                              @click="() => deleteDep(data)">
                        删除
                      </el-button>
                    </span>
                </span>
        </el-tree>
        <!--编辑弹出框-->
        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%">
            <table>
                <tr>
                    <td>
                        <el-tag type="success">上级部门</el-tag>
                    </td>
                    <td>
                        <span style="margin-left: 10px">{{pname}} </span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <el-tag type="success">部门名称</el-tag>
                    </td>
                    <td>
                      <el-input size="small" v-model="addDep.name" style="margin-left: 8px" placeholder="请输入部门名称..."></el-input>
                    </td>
                </tr>
            </table>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doAddDep">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>
    import {initMenu} from "../../../utils/menus";

    export default {
        name: "DepMana",
        data() {
            return {
                filterText: '',
                deps: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                dialogVisible:false,
                addDep:{
                    name:'',
                    parentid:''
                },
                pname:''
            }
        },
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            }
        },
        mounted() {
            this.initDeps();
        },
        methods: {
            initDep(){
                this.addDep={
                    name:'',
                    parentid: -1
                }
                this.pname=''
            },
            addDep2Deps(deps,dep){
              for(let i= 0;i<deps.length;i++){
                  let d = deps[i];
                  if(d.id === dep.parentid){
                      d.children = d.children.concat(dep)
                      if(d.children.length>0){
                          d.isparent = true;
                      }
                      return;
                  }else {
                      this.addDep2Deps(d.children,dep)
                  }
              }
            },
            doAddDep(){
                this.postRequest('/system/basic/department/',this.addDep).then(resp=>{
                    if (resp) {
                        this.addDep2Deps(this.deps,resp.obj)
                        this.dialogVisible=false
                        this.initDep();
                    }
                })
            },
            showAddDep(data){
                this.pname=data.name
                this.addDep.parentid=data.id
                this.dialogVisible =true;
            },
            removeFormDeps(p,deps,id){
                for(let i= 0;i<deps.length;i++){
                    let d = deps[i];
                    if(d.id === id){
                        deps.splice(i,1)
                        if(deps.length===0){
                            p.isparent = false
                        }
                        return;
                    }else {
                        this.removeFormDeps(d,d.children,id)
                    }
                }
            },
            deleteDep(data){
                if(data.isparent===true){
                    this.$message.error('该部门有子部门，删除失败')
                }else {
                    this.$confirm('此操作将永久删除该[' + data.name + ']部门， 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.deleteRequest('/system/basic/department/' + data.id).then(resp => {
                            if (resp) {
                                this.removeFormDeps(null,this.deps,data.id);
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
            },
            initDeps() {
                this.getRequest('/system/basic/department/').then(resp => {
                    this.deps = resp;
                })
            },
            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            }
        },

    }
</script>

<style scoped>
    .depNode {
        padding: 3px;
    }
</style>